using Avalonia.Controls;
using System;
using System.Collections.Generic;

namespace AvaloniaDrawingPad.Models.Commands
{
    /// <summary>
    /// 删除图形命令
    /// </summary>
    public class DeleteShapeCommand : ICommand
    {
        private readonly Layer _layer;
        private readonly Control _shape;
        private readonly SelectableShape _selectableShape;
        private readonly LayerManager _layerManager;
        private readonly List<SelectableShape> _selectableShapes;
        private readonly Action? _onShapeDeleted;
        
        public string Description => "删除图形";
        
        public DeleteShapeCommand(
            Layer layer, 
            Control shape, 
            SelectableShape selectableShape, 
            LayerManager layerManager,
            List<SelectableShape> selectableShapes,
            Action? onShapeDeleted = null)
        {
            _layer = layer;
            _shape = shape;
            _selectableShape = selectableShape;
            _layerManager = layerManager;
            _selectableShapes = selectableShapes;
            _onShapeDeleted = onShapeDeleted;
        }
        
        public void Execute()
        {
            // 确保先清除选择状态，移除选择框UI元素
            if (_selectableShape.IsSelected)
            {
                _selectableShape.IsSelected = false;
            }
            
            // 从图层中移除图形
            _layer.Content.Children.Remove(_shape);
            
            // 从可选择图形列表中移除
            _selectableShapes.Remove(_selectableShape);
            
            // 如果有回调，执行回调
            _onShapeDeleted?.Invoke();
            
            // 标记项目已修改
            _layerManager.MarkAsChanged();
        }
        
        public void Undo()
        {
            // 添加图形到图层
            _layer.Content.Children.Add(_shape);
            
            // 添加到可选择图形列表
            _selectableShapes.Add(_selectableShape);
            
            // 标记项目已修改
            _layerManager.MarkAsChanged();
        }
    }
} 